home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / c / MemPools.lha / mempools / TimeMem.c < prev    next >
C/C++ Source or Header  |  1994-12-05  |  4KB  |  204 lines

  1. /**
  2. ***  MemPools:    malloc() replacement using standard Amiga pool functions.
  3. ***  Copyright    (C)  1994    Jochen Wiedmann
  4. ***
  5. ***  This program is free software; you can redistribute it and/or modify
  6. ***  it under the terms of the GNU General Public License as published by
  7. ***  the Free Software Foundation; either version 2 of the License, or
  8. ***  (at your option) any later version.
  9. ***
  10. ***  This program is distributed in the hope that it will be useful,
  11. ***  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ***  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ***  GNU General Public License for more details.
  14. ***
  15. ***  You should have received a copy of the GNU General Public License
  16. ***  along with this program; if not, write to the Free Software
  17. ***  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ***
  19. ***
  20. ***  This is a very simple timing and test program for the memory
  21. ***  functions of stdlib.h.
  22. ***
  23. ***
  24. ***  Computer:    Amiga 1200
  25. ***
  26. ***  Compilers: Dice 3.01
  27. ***        SAS/C 6.3
  28. ***        gcc 2.6.1
  29. ***
  30. ***
  31. ***  Author:    Jochen Wiedmann
  32. ***        Am Eisteich 9
  33. ***      72555 Metzingen
  34. ***        Germany
  35. ***
  36. ***        Phone: (0049) 7123 14881
  37. ***        Internet: jochen.wiedmann@uni-tuebingen.de
  38. **/
  39.  
  40.  
  41.  
  42.  
  43. /*
  44.     Include files and compiler specific stuff
  45. */
  46. #include <stdlib.h>
  47. #include <stdio.h>
  48. #include <string.h>
  49.  
  50. #ifndef FALSE
  51. #define FALSE 0
  52. #endif
  53. #ifndef TRUE
  54. #define TRUE (!FALSE)
  55. #endif
  56.  
  57. #if defined(__GNUC__)
  58. #define stricmp strcasecmp
  59. #define strnicmp strncasecmp
  60. #endif
  61.  
  62.  
  63.  
  64.  
  65.  
  66. /**
  67. ***  It is important that these numbers are powers of 2, as
  68. ***  they are used as arguments for rangerand()!
  69. **/
  70. #define NUMALLOCS 8192
  71. #define MAXSIZE 128
  72.  
  73.  
  74.  
  75. int rangerand(int max)
  76.  
  77. { int result = rand();
  78.  
  79.   while(result >= max)
  80.   { result = result >> 1;
  81.   }
  82.   return(result);
  83. }
  84.  
  85.  
  86. typedef void *APTR;
  87.  
  88.  
  89.  
  90. int __MemPoolPuddleSize = 16384;
  91.  
  92.  
  93. int main(int argc, char *argv[])
  94.  
  95. { int i, j;
  96.   int malloc_cnt = 0, calloc_cnt = 0, realloc_cnt = 0;
  97.   int malloc_free = 0, calloc_free = 0, realloc_free = 0;
  98.   APTR *malloc_table;
  99.   APTR *calloc_table;
  100.   APTR *realloc_table;
  101.  
  102.   int VerboseMode = FALSE;
  103.  
  104.   srand(27);
  105.  
  106.   for (i = 1;  i < argc;  i++)
  107.   { if ((stricmp(argv[i], "?") == 0       ||
  108.      stricmp(argv[i], "help") == 0    ||
  109.      stricmp(argv[i], "-h") == 0))
  110.     { printf("Usage: TimeMem [VERBOSE}\n");
  111.       exit(5);
  112.     }
  113.     if (stricmp(argv[i], "verbose") == 0    ||
  114.     stricmp(argv[i], "-v") == 0)
  115.     { VerboseMode = TRUE;
  116.     }
  117.     else
  118.     { fprintf(stderr, "Unknown option: %s\n", argv[i]);
  119.     }
  120.   }
  121.  
  122.   if (!(malloc_table = calloc(NUMALLOCS, sizeof(APTR)))  ||
  123.       !(calloc_table = calloc(NUMALLOCS, sizeof(APTR)))  ||
  124.       !(realloc_table = calloc(NUMALLOCS, sizeof(APTR))))
  125.   { perror("malloc");
  126.   }
  127.  
  128.   for (i = 0;  i < NUMALLOCS;  i++)
  129.   {
  130. #ifdef DEBUG
  131.     printf("i = %d\n", i);
  132. #endif
  133.     /**
  134.     ***  Call malloc()
  135.     **/
  136.     if (!(malloc_table[i] = malloc(rangerand(MAXSIZE)+1)))
  137.     { perror("malloc");
  138.       exit(10);
  139.     }
  140.     malloc_cnt++;
  141.  
  142.     /**
  143.     ***  Call calloc()
  144.     **/
  145.     if (!(calloc_table[i] = calloc(rangerand(MAXSIZE)+1, 1)))
  146.     { perror("calloc");
  147.       exit(10);
  148.     }
  149.     calloc_cnt++;
  150.  
  151.     /**
  152.     ***  Call realloc()
  153.     **/
  154.     j = rangerand(NUMALLOCS);
  155.     if (!(realloc_table[j] = realloc(realloc_table[j], rangerand(MAXSIZE)+1)))
  156.     { perror("malloc");
  157.       exit(10);
  158.     }
  159.     realloc_cnt++;
  160.  
  161.     /**
  162.     ***  Free a block of memory obtained with malloc().
  163.     **/
  164.     j = rangerand(NUMALLOCS);
  165.     if (malloc_table[j])
  166.     { free(malloc_table[j]);
  167.       malloc_table[j] = NULL;
  168.       malloc_free++;
  169.     }
  170.  
  171.     /**
  172.     ***  Free a block of memory obtained with calloc().
  173.     **/
  174.     j = rangerand(NUMALLOCS);
  175.     if (calloc_table[j])
  176.     { free(calloc_table[j]);
  177.       calloc_table[j] = NULL;
  178.       calloc_free++;
  179.     }
  180.  
  181.     /**
  182.     ***  Free a block of memory obtained with realloc().
  183.     **/
  184.     j = rangerand(NUMALLOCS);
  185.     if (realloc_table[j])
  186.     { free(realloc_table[j]);
  187.       realloc_table[j] = NULL;
  188.       realloc_free++;
  189.     }
  190.   }
  191.  
  192.   if (VerboseMode)
  193.   { printf("TimeMem statistics:\n\n");
  194.     printf("Calls to malloc(): %d\n", malloc_cnt);
  195.     printf("Calls to calloc(): %d\n", calloc_cnt);
  196.     printf("Calls to realloc(): %d\n", realloc_cnt);
  197.     printf("Calls to free() for blocks obtained by malloc(): %d\n", malloc_free);
  198.     printf("Calls to free() for blocks obtained by calloc(): %d\n", calloc_free);
  199.     printf("Calls to free() for blocks obtained by realloc(): %d\n", realloc_free);
  200.   }
  201.  
  202.   exit(0);
  203. }
  204.